﻿using Microsoft.Office.Tools;

namespace ExcelPoSH
{
    public partial class ThisAddIn
    {
        private Console mConsole;
        private CustomTaskPane mPane;

        public Console console
        {
            get
            {
                return mConsole;
            }
        }

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            // create an instance of Console
            // and add it to Excel custom task panes collection
            
            mConsole = new Console(Globals.ThisAddIn.Application);
            mPane = this.CustomTaskPanes.Add(mConsole, "PowerShell Console");
            mPane.DockPosition = Microsoft.Office.Core.
                MsoCTPDockPosition.msoCTPDockPositionFloating;
            mPane.Height = 300;
            mPane.Width = 400;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        // This is called from the ribbon to show the pane

        public void ShowConsole()
        {
            mPane.Visible = true;
            ConsoleSetFocus();
        }

        // This is called from the ribbon to hide pane

        public void HideConsole()
        {
            mPane.Visible = false;
        }

        // When showing - hiding the pane from the ribbon
        // loses focus, so I needed this.

        public void ConsoleSetFocus()
        {
            Globals.ThisAddIn.Application.
                CommandBars["PowerShell Console"].Controls[1].SetFocus();
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
